package cn.project.engineering.ui.adapter

import android.text.Editable
import android.text.TextWatcher
import android.widget.CheckBox
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.Toast
import cn.project.engineering.R
import cn.project.engineering.http.bean.StationInfo
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lxj.xpopup.XPopup
import log.KLog


class StationListDistributionDapter(layoutid: Int, data: MutableList<StationInfo>) : BaseQuickAdapter<StationInfo, BaseViewHolder>(layoutid,data) {
     var etFocusPositon=0


    var textWatcher: TextWatcher = object : TextWatcher {
        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {

        }
        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {

        }
        override fun afterTextChanged(s: Editable) {
            //没次更改文字，保存集合中
            updata(s.toString())
//            data.get(etFocusPositon).CurrentPage=s.toString();
        }
    }
     override fun convert(holder: BaseViewHolder, item: StationInfo) {
         val view = holder.getView<CheckBox>(R.id.select_station)
        view.isChecked=item.Choose
         view.setOnClickListener {
             item.Choose=view.isChecked
             if(view.isChecked){
                 for (i in 0 until data.size){
                     KLog.d("DistributionActivity", data.get(i).CurrentPage)
                     if (i==holder.layoutPosition)
                     {
                         continue
                     }
                     data.get(i).Choose=false
                 }
             }
             KLog.d("DistributionActivity", data.toString())
             notifyDataSetChanged()
         }

         holder.setText(R.id.num_station,item.FNumber)
         holder.setText(R.id.name_station,item.FName)
         holder.setText(R.id.name_file_station,item.CurrentFileName)
         holder.setText(R.id.numfile_station,item.FileNO)

         val editText = holder.getView<EditText>(R.id.numpage_station)
         editText.setText(item.CurrentPage)
         editText.setOnFocusChangeListener { v, hasFocus ->
             if (hasFocus){
                 etFocusPositon=holder.adapterPosition
                 //添加监听
                 editText.addTextChangedListener(textWatcher)
             }else{
                //移除监听器
                 editText.removeTextChangedListener(textWatcher)
             }
         }
         val viewLL = holder.getView<LinearLayout>(R.id.distribution_ll)
         val builder = XPopup.Builder(context)
                 .watchView(viewLL)
         viewLL.setOnClickListener {
             builder.asAttachList(arrayOf("添加一行", "删除一行"), null)
             { position, text -> (

                 when (position) {
                     0 -> {
                         var itemnew:StationInfo= StationInfo(false,item.FNumber,item.FName,"","","","","",item.LineNO)
                         addData(holder.layoutPosition+1,itemnew)
                         KLog.d("DistributionActivity", data.toString())

                     }
                     1 -> {
                        remove(holder.layoutPosition)
                         KLog.d("DistributionActivity", data.toString())
                     }
                 })

             }.show()
         }


     }
    fun updata(content:String){
        data.get(etFocusPositon).CurrentPage=content;

    }
 }